let express = require('express');
let mysql = require('./db')

let router = express.Router();
let  jwt = require('jsonwebtoken')


router.get('/managerlist', (req, res) => {
    let { page=1, limit=10 } = req.query;
    let index = (page - 1) * limit;
    
    mysql.query(`select * from manager limit ${index},${limit};`, function (state, data) {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.post('/login', (req, res) => {
    let { manager, password } = req.body;
    var authorization =jwt.sign(manager,'quexue')
    mysql.query(`select * from manager where manager='${manager}' and password='${password}';`, (state, data) => {
        data[0].authorization =authorization
        data=data[0]
        if (state) {
            if (data.length == 0) {
                var msg = '用户名或密码错误';
                var code = 400;
            } else {
                var msg = '登录成功'
                var code = 200;
            }
            res.send({
                code,
                msg: msg,
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '出错了',
                data
            })
        }
    })
})

// /manager/reg
router.post('/reg', (req, res) => {
    let { manager, password } = req.body;
    mysql.query(`insert into manager values(NULL, '${manager}', '${password}');`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '注册成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '注册失败',
                data
            })
        }
    })
})

router.post('/remove/:id', (req, res) => {
    let { id } = req.params;
    mysql.query(`DELETE FROM manager where manager_id=${id}`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '删除成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '删除失败',
                data
            })
        }
    })
})

router.get('/managers', (req, res) => {
    mysql.query(`select * from manager;`, function (state, data) {
        if (state) {
            res.send({
                code: 200,
                msg: 'success',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: 'error',
                data
            })
        }
    })
})

router.put('/change/:id', (req, res) => {
    let {id}=req.params
    let {manager,password} = req.body
    mysql.query(`update user set manager='${manager}'  where user_id='${id}';update user set password='${password}'  where manager_id='${id}'`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '修改成功',
                data
            })
        } else {
            res.send({
                code: 400,
                msg: '修改失败',
            })
        }
    })
})

router.post('/check', (req, res) => {
    let { manager } = req.body;
    mysql.query(`select * from manager where manager='${manager}';`, (state, data) => {
        if (state) {
            if (data.length == 0) {
                var msg = '可以注册';
                var code = 200;
            } else {
                var msg = '该用户已经存在';
                var code = 400;
            }
            res.send({
                code,
                msg
            })
        } else {
            res.send({
                code: 400,
                msg: 'error'
            })
        }
    })
})

router.post('/remove', (req, res) => {
    let { data } = req.body;
    let {ids} = data
    let id=ids.join(',')
        mysql.query(`DELETE FROM manager where manager_id in(${id}) `, (state, data) => {
            if (state) {
                res.send({
                    code: 200,
                    msg: '删除成功',
                    data
                })
            } else {
                res.send({
                    code: 400,
                    msg: '删除失败',
                    data
                })
            }
        })
    
})


module.exports = router;